package org.ttitfly.workflow.engine.basic;

import java.util.List;

import org.ttitfly.workflow.engine.Action;
import org.ttitfly.workflow.exception.ProcessRunException;
import org.ttitfly.workflow.handler.Handler;
import org.ttitfly.workflow.handler.ListHandlerInvocation;


/**
 * 动作的基本功能实现
 * 
 * @author 潘海春
 * @version 2009-9-22
 */
public class BasicAction implements Action {
	
	/**
	 * 默认的执行结果状态
	 */
	private static final String DEFAULT_STATUS = "FINISHED";

	/**
	 * 名称
	 */
	private String name;
	
	/**
	 * 执行结果状态
	 */
	private String status = DEFAULT_STATUS;

	/**
	 * handlers
	 */
	private List<Handler> handlers;

	/**
	 * 指向的下一个单元名称
	 */
	private String result;

	/**
	 * 构造函数
	 * 
	 * @param name
	 *            名称，不能为空
	 * @param handlers
	 *            处理器
	 * @param result
	 *            下个单元名称
	 * @param status
	 *            执行结果状态
	 */
	public BasicAction(String name, List<Handler> handlers, String result, String status) {
		if (name == null || "".equals(name = name.trim())) {
			throw new IllegalArgumentException("参数[name]不能为空！");
		}
		this.name = name;
		this.handlers = handlers;
		this.result = result;
		if (status != null) {
			this.status = status;
		}
	}

	@Override
	public String execute() throws ProcessRunException {
		if (handlers != null && !handlers.isEmpty()) {
			try {
				new ListHandlerInvocation(handlers).proceed();
			} catch (Exception e) {
				throw new ProcessRunException(e);
			}
		}
		return result;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getStatus() {
		return status;
	}

}
